package cn.trigram.lang;

import java.util.Arrays;

public class ArrayUtil {

  public static String byteArrayToString(byte[] arrays) {

    if (ObjectUtil.isNull(arrays)) {
      return StringUtil.EMPTY;
    }
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < arrays.length; i++) {
      Object t = arrays[i];
      str.append(t);
    }
    return str.toString();
  }

  public static String printArrayValue(Object[] arrays) {

    if (ObjectUtil.isNull(arrays)) {
      return StringUtil.EMPTY;
    }
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < arrays.length; i++) {
      Object t = arrays[i];
      str.append(t);
    }
    return str.toString();
  }

  public static String toString(Object obj) {

    if (obj == null) {
      return "null";
    }
    final Class<?> clazz = obj.getClass();
    if (!clazz.isArray()) {
      return obj.toString();
    }
    final Class<?> componentType = obj.getClass().getComponentType();
    if (long.class.equals(componentType)) {
      return Arrays.toString((long[]) obj);
    } else if (int.class.equals(componentType)) {
      return Arrays.toString((int[]) obj);
    } else if (short.class.equals(componentType)) {
      return Arrays.toString((short[]) obj);
    } else if (char.class.equals(componentType)) {
      return Arrays.toString((char[]) obj);
    } else if (byte.class.equals(componentType)) {
      return Arrays.toString((byte[]) obj);
    } else if (boolean.class.equals(componentType)) {
      return Arrays.toString((boolean[]) obj);
    } else if (float.class.equals(componentType)) {
      return Arrays.toString((float[]) obj);
    } else if (double.class.equals(componentType)) {
      return Arrays.toString((double[]) obj);
    } else {
      return Arrays.toString((Object[]) obj);
    }
  }

}
